package com.common.base.utils.span

import android.graphics.Typeface
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import com.common.base.utils.AppUtils

/**
 * @since 2019/2/5 16:28
 * 文字Span处理助手
 */
object TextSpanHelper {

    /**
     * 获取数字字体Span构造器
     */
    private fun getNewNumberFontTypeface(): Typeface {
        return Typeface.createFromAsset(AppUtils.getContext().assets, "font/DINCondensedBold.ttf")
    }

    fun getNewNumberFontSpanBuilder(number: Int): SpannableStringBuilder {
        return getNewNumberFontSpanBuilder(number.toString())
    }

    fun getNewNumberFontSpanBuilder(text: String): SpannableStringBuilder {
        val builder = SpannableStringBuilder(text)
        builder.setSpan(NumberTypefaceSpan("", getNewNumberFontTypeface()), 0, text.length, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        return builder
    }

    fun getNumberFontSpanBuilder(number: Int): SpannableStringBuilder {
        return getNumberFontSpanBuilder(number.toString())
    }

    fun getNumberFontSpanBuilder(text: String): SpannableStringBuilder {
        val builder = SpannableStringBuilder(text)
        builder.setSpan(NumberTypefaceSpan("", getNewNumberFontTypeface()), 0, text.length, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        return builder
    }

    fun getColorSpanBuilder(text: String, start: Int, end: Int, color: Int): SpannableStringBuilder {
        val builder = SpannableStringBuilder(text)
        // 设置字体前景色
        builder.setSpan(ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return builder
    }

    fun getColorSpanBuilderClick(clickableSpan: ClickableSpan, nickName: String?, text: String, start: Int, end: Int?, color: Int, nameColor: Int): SpannableStringBuilder {
        val builder = SpannableStringBuilder(text)
        if (nickName != null && end != null) {
            // 设置字体前景色
            builder.setSpan(clickableSpan, start, nickName.length + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            builder.setSpan(ForegroundColorSpan(nameColor), start, nickName.length + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            builder.setSpan(ForegroundColorSpan(color), nickName.length + 1, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        return builder
    }

    fun getColorSpanBuilder(text: String, color: Int): SpannableStringBuilder {
        return getColorSpanBuilder(text, 0, text.length, color)
    }

    // 设置字体大小的span, size: dp
    fun getAbsoluteSizeSpanBuilder(text: String, size: Int): SpannableStringBuilder {
        return getAbsoluteSizeSpanBuilder(text, size, 0, text.length)
    }

    // 设置字体大小的span, size: dp
    fun getAbsoluteSizeSpanBuilder(text: String, size: Int, start: Int, end: Int): SpannableStringBuilder {
        val builder = SpannableStringBuilder(text)
        builder.setSpan(AbsoluteSizeSpan(size, true), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return builder
    }

    fun getColorSpanBuilderClickV2(clickableSpan: ClickableSpan, nickName: String?, text: String, start: Int, end: Int?, color: Int, nameColor: Int): SpannableStringBuilder {
        val builder = SpannableStringBuilder(text)
        if (nickName != null && end != null) {
            // 设置字体前景色
            builder.setSpan(clickableSpan, start, nickName.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            builder.setSpan(ForegroundColorSpan(nameColor), start, nickName.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            builder.setSpan(ForegroundColorSpan(color), nickName.length, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        return builder
    }
}